package hik;

import java.io.BufferedReader;
import java.io.FileReader;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class ParseXml {

	//用Element方式
    public static void element(NodeList list){
        for (int i = 0; i <list.getLength() ; i++) {
            Element element = (Element) list.item(i);
            NodeList childNodes = element.getChildNodes();
            for (int j = 0; j <childNodes.getLength() ; j++) {
                if (childNodes.item(j).getNodeType()==Node.ELEMENT_NODE) {
                    //获取节点
                    System.out.print(childNodes.item(j).getNodeName() + ":");
                    //获取节点值
                    System.out.println(childNodes.item(j).getFirstChild().getNodeValue());
                }
            }
        }
    }

    public static void node(NodeList list){
        for (int i = 0; i <list.getLength() ; i++) {
            Node node = list.item(i);
            NodeList childNodes = node.getChildNodes();
            for (int j = 0; j <childNodes.getLength() ; j++) {
                if (childNodes.item(j).getNodeType()==Node.ELEMENT_NODE) {
                    System.out.print(childNodes.item(j).getNodeName() + ":");
                    System.out.println(childNodes.item(j).getFirstChild().getNodeValue());
                }
            }
        }
    }
    
    
    public static String readXml(String filePath) {
    	StringBuffer sb = new StringBuffer();
        
    	FileReader fileReader = null;
		BufferedReader bufferedReader = null;
		try {
			fileReader = new FileReader(filePath);

			  bufferedReader = new BufferedReader(fileReader);
			  
			  String line =bufferedReader.readLine();
			  

			  while (line!=null){
				  sb.append(line);
			      line = bufferedReader.readLine();
			  }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				bufferedReader.close();
				fileReader.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return sb.toString();
          
    }

    public static void main(String[] args) {
//        //1.创建DocumentBuilderFactory对象
//        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//        //2.创建DocumentBuilder对象
//        try {
//            DocumentBuilder builder = factory.newDocumentBuilder();
//            Document d = builder.parse("src/main/resources/demo.xml");
//            NodeList sList = d.getElementsByTagName("student");
//            //element(sList);
//            node(sList);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    	String path = "C:\\setPresetInfo.xml";
    	System.out.println(readXml(path));
    }
}
